package offer;

/**
 * @Author Elephas
 * @Date 2022/1/13
 **/
public class ReversePrint {
    /*
        解法一：两次遍历，时间复杂度o(n),空间复杂度o(1)
     */

    public int[] reversePrint(ListNode Head){
        int nodeNums = 0;
        ListNode cur = Head;
        while(cur != null){
            nodeNums++;
            cur = cur.next;
        }
        int[] ans = new int[nodeNums];
        cur = Head;
        for (int i = 0; i < nodeNums; i++) {
            ans[nodeNums-i-1] = cur.val;
            cur = cur.next;
        }
        return ans;
    }
}
